package com.banksoft.cps.service.impl;

import com.banksoft.cps.bean.FileInfo;
import com.banksoft.cps.bean.PoliceCheck;
import com.banksoft.cps.bean.PoliceCheckTableOne;
import com.banksoft.cps.bean.warpper.PoliceCheckTableOneWrapper;
import com.banksoft.cps.dao.FileInfoDao;
import com.banksoft.cps.dao.PoliceCheckDao;
import com.banksoft.cps.dao.PoliceCheckTableOneDao;
import com.banksoft.cps.service.PoliceCheckTableOneService;
import com.banksoft.cps.util.UUIDFactory;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by rongzhao.zhu on 2018/11/21.
 */
@Service
public class PoliceCheckTableOneServiceImpl implements PoliceCheckTableOneService {
    @Autowired
    PoliceCheckDao policeCheckDao;
    @Autowired
    PoliceCheckTableOneDao policeCheckTableOneDao;
    @Autowired
    FileInfoDao fileInfoDao;
    @Override
    public void save(PoliceCheckTableOne bean) {
        if(StringUtils.isNotBlank(bean.getId())){
            policeCheckTableOneDao.update(bean);
        }else{
            bean.setId(UUIDFactory.random());
            policeCheckTableOneDao.insert(bean);
            PoliceCheck policeCheck= policeCheckDao.findById(bean.getPoliceCheckId());
            policeCheck.setTable1(bean.getId());
            policeCheckDao.update(policeCheck);

        }
    }

    @Override
    public PoliceCheckTableOne findById(String id) {
            return policeCheckTableOneDao.findById(id);
    }

    @Override
    public PoliceCheckTableOneWrapper findWrapperById(String id) {
        PoliceCheckTableOne bean = policeCheckTableOneDao.findById(id);
        PoliceCheckTableOneWrapper wrapper = new PoliceCheckTableOneWrapper();
        BeanUtils.copyProperties(bean,wrapper);
        List<String> fileIds = new ArrayList<>();

        addFileId(fileIds,bean.getTroubleFile());
        addFileId(fileIds,bean.getSuggestFile());
        addFileId(fileIds,bean.getReceiptFile());

        Map<String,FileInfo> fileInfoMap ;
        if(!fileIds.isEmpty()){
            List<FileInfo> fileInfos = fileInfoDao.queryFiles(fileIds);
            fileInfoMap = fileInfos.stream().collect(Collectors.toMap(e->e.getId(), e->e));
        }else {
            fileInfoMap = new HashMap<>();
        }
        List<FileInfo> troubleFileList = getFileList(fileInfoMap,bean.getTroubleFile());
        List<FileInfo> suggestFileList = getFileList(fileInfoMap,bean.getSuggestFile());
        List<FileInfo> receiptFileList = getFileList(fileInfoMap,bean.getReceiptFile());

        wrapper.setReceiptFileList(receiptFileList);
        wrapper.setSuggestFileList(suggestFileList);
        wrapper.setTroubleFileList(troubleFileList);
        return wrapper;
    }

    private void addFileId(List<String> fileIds,String fileIdsStr){
        if(StringUtils.isNotBlank(fileIdsStr)){
            String[] fileIdsArray = fileIdsStr.split(",");
            fileIds.addAll(Arrays.asList(fileIdsArray)) ;
        }
    }
    private  List<FileInfo> getFileList(Map<String,FileInfo> fileInfoMap,String fileIdsStr){
        List<FileInfo> fileList = new ArrayList<>();
        if(StringUtils.isNotBlank(fileIdsStr)){
            String[] fileIdsArray = fileIdsStr.split(",");
            for(String _id :fileIdsArray){
                FileInfo _fileInfo =  fileInfoMap.get(_id);
                if(_fileInfo !=null){
                    fileList.add(_fileInfo) ;
                }

            }
        }
        return fileList;
    }
}
